﻿using System;
using CSharpLogger.Common;

namespace CSharpLogger.Client
{
    public static class LoggerHandlerExtension
    {
        public static void LogTo(this ILoggerHandler logger, string message, LoggerType type = LoggerType.FILE, LogLevel level = LogLevel.INFO)
        {
            if ((type & LoggerType.FILE) == LoggerType.FILE)
            {
                logger = FileLogger.Logger;
                logger.Log(message, level);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="message"></param>
        public static void Debug(this ILoggerHandler logger, string message)
        {
            logger.Log(message, LogLevel.DEBUG);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="message"></param>
        public static void Warn(this ILoggerHandler logger, string message)
        {
            logger.Log(message, LogLevel.WARN);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="message"></param>
        public static void Fatal(this ILoggerHandler logger, string message)
        {
            logger.Log(message, LogLevel.FATAL);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="message"></param>
        public static void Info(this ILoggerHandler logger, string message)
        {
            logger.Log(message, LogLevel.INFO);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="message"></param>
        public static void Error(this ILoggerHandler logger, string message)
        {
            logger.Log(message, LogLevel.ERROR);
        }

        public static void LogException(this ILoggerHandler logger, Exception e)
        {
            if (e == null)
                return;

            var message = e.Message;

            var inner = e.InnerException != null ? e.InnerException.Message : null;

            var msg = string.Format("Exception: {0}  Inner Exception:{1}", message, inner);

            logger.Log(msg, LogLevel.ERROR);

        }
    }
}
